<?php
include_once '../Utilities/connect.php';
$conn=  getConnect();
mysql_set_charset('utf8',$conn);
function getRecordArticle($display)
{
    $conn=  getConnect();
    $sql="select count(ArticleID) from article";
    $res=  mysql_query($sql, $conn) or die('Could not query excuted');
    $rows=  mysql_fetch_array($res, MYSQL_NUM);
    $record=$rows[0];
    if($record>$display)
        $page=  ceil ($record/$display);
    else
        $page=1;
    return $page;
    mysql_close($conn);
}
function getRecordAllowable($display)
{
    $conn=  getConnect();
    $sql="select count(ArticleID) from article where Allowable=1";
    $res=  mysql_query($sql, $conn) or die('Could not query excuted');
    $rows=  mysql_fetch_array($res, MYSQL_NUM);
    $record=$rows[0];
    if($record>$display)
        $page=  ceil ($record/$display);
    else
        $page=1;
    return $page;
    mysql_close($conn);
}
function getArticleSplit($start,$display)
{
      $conn=  getConnect();
    $sql= "select * from Article order by CreatedDate desc limit $start,$display";
		$rl=mysql_query($sql, getConnect());
		$arr=array();
		while ($row = mysql_fetch_array($rl))
		{
		 	$arr[]=$row;
		}
		return $arr;
    mysql_close($conn);
}
function InsertArticle($title, $content, $author,$createddate,$image,$user)
{
        $conn=  getConnect();
	$sql= "insert into article(Title, Content, Author,CreatedDate,Image,UserName ) values('$title','$content','$author','$createddate','$image','$user')";
        $result=  mysql_query($sql,$conn);
        return $result;
        mysql_close($conn);
}
function getIDmax()
{
     $conn=  getConnect();
    $sql= "select max(ArticleID) from Article";
		$rl=mysql_query($sql, getConnect());
		$arr=array();
		while ($row = mysql_fetch_array($rl))
		{
		 	$arr[]=$row;
		}
		return $arr;
    mysql_close($conn);
}
function getArticle($ArticleID)
{
     $conn=  getConnect();
    $sql= "select Title,Content,Author,Image,ArticleID from Article where ArticleID='$ArticleID'";
		$rl=mysql_query($sql, getConnect());
		$arr=array();
		while ($row = mysql_fetch_array($rl))
		{
		 	$arr[]=$row;
		}
		return $arr;
    mysql_close($conn);
}
function getArticle_User($UserName)
{
     $conn=  getConnect();
    $sql= "select ArticleID,Title,Content,Author,Image,CreatedDate from Article where ArticleID=(Select Max(ArticleID) from article where UserName='$UserName' )";
		$rl=mysql_query($sql, getConnect());
		$arr=array();
		while ($row = mysql_fetch_array($rl))
		{
		 	$arr[]=$row;
		}
		return $arr;
    mysql_close($conn);
}
function getAllArticle()
{
     $conn=  getConnect();
    $sql= "select * from Article order by CreatedDate desc";
		$rl=mysql_query($sql, getConnect());
		$arr=array();
		while ($row = mysql_fetch_array($rl))
		{
		 	$arr[]=$row;
		}
		return $arr;
    mysql_close($conn);
}
function getArticleAllow($start,$display)
{
     $conn=  getConnect();
    $sql= "select * from Article where Allowable=1 order by CreatedDate desc limit $start,$display";
		$rl=mysql_query($sql, getConnect());
		$arr=array();
		while ($row = mysql_fetch_array($rl))
		{
		 	$arr[]=$row;
		}
		return $arr;
    mysql_close($conn);
}
function getAllow()
{
     $conn=  getConnect();
    $sql= "select * from Article where Allowable=1 order by CreatedDate desc";
		$rl=mysql_query($sql, getConnect());
		$arr=array();
		while ($row = mysql_fetch_array($rl))
		{
		 	$arr[]=$row;
		}
		return $arr;
    mysql_close($conn);
}
function DeleteArticle($ArticleID)
{
         $conn=  getConnect();
         if(!$conn)
             die("Error connect");
	$sql= "delete from article where ArticleID='$ArticleID'";    
        $result=  mysql_query($sql,$conn);
        if(!$result)
            die("error query");
        return $result;
        mysql_close($conn);
}
function UpdateArticle($title, $content,$author,$modifiedDate,$articleID)
{
     $conn=  getConnect();
//	$sql= "update product set ProductName='$name',Description='$description',Price='$price',Size='$size',CategpryID='$categoryID' where ProductID='$productID'";
     $sql="UPDATE article SET Title='$title',Content='$content',Author='$author',ModifiedDate=$modifiedDate WHERE ArticleID='$articleID'";
        $result=  mysql_query($sql,$conn);
        if(!$result)
            die('error query');
        return $result;
        mysql_close($conn);
}
function UpdateAllow($Allowable,$articleID)
{
     $conn=  getConnect();
//	$sql= "update product set ProductName='$name',Description='$description',Price='$price',Size='$size',CategpryID='$categoryID' where ProductID='$productID'";
     $sql="UPDATE article SET Allowable='$Allowable' WHERE ArticleID='$articleID'";
        $result=  mysql_query($sql,$conn);
        if(!$result)
            die('error query');
        return $result;
        mysql_close($conn);
}
  function InsertComment($content,$name,$email,$createdDate,$ArticleID)
    {
        $conn=  getConnect();
	$sql= "insert into commentarticle(Content, Name, Email,CreatedDate,ArticleID ) values('$content','$name','$email','$createdDate','$ArticleID')";
        $result=  mysql_query($sql,$conn);
        if(!$result)
            die('Error Query'.  mysql_error($conn));
        return $result;
        mysql_close($conn);
    }
 function CommentCount($ArticleID)
 {
      $conn=  getConnect();
      $count;
      $sql="select count(*) from commentarticle where ArticleID='$ArticleID'";
      $result=  mysql_query($sql, $conn);
      if(!$result)
          die('Error query');
      while($row=  mysql_fetch_array($result))
          $count=$row[0];
      return $count;
      mysql_close($conn);
 }
 function getComment($ArticleID)
 {
      $conn=  getConnect();
      $arr=array();
      $sql="select * from commentarticle where ArticleID='$ArticleID'";
      $result=  mysql_query($sql, $conn);
      if(!$result)
          die('Error query');
      while($row=  mysql_fetch_array($result))
      {
          $arr[]=$row;
      }
      return $arr;
      mysql_close($conn);
 }
?>
